<template>
  <div class="page">
    <div class="login">
      <div class="titles">
        <div class="title-main">Matcha</div>
        <div class="title-sub">管理系统</div>
      </div>
      <el-form ref="loginForm" :rules="rules" :model="loginForm" status-icon>
        <el-form-item size="medium" prop="username">
          <el-input placeholder="账号" prefix-icon="el-icon-user" v-model="loginForm.username" autofocus />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            size="medium"
            type="password"
            placeholder="密码"
            prefix-icon="el-icon-lock"
            auto-complete="off"
            show-password
            v-model="loginForm.password"
          />
        </el-form-item>
        <el-form-item prop="captcha" class="form-code">
          <el-input
            size="medium"
            placeholder="验证码"
            prefix-icon="el-icon-thumb"
            v-model="loginForm.captcha"
            @keyup.enter.native="handleLogin"
          />
          <span class="codeimg" v-html="captcha" @click="getCaptcha"> </span>
        </el-form-item>
        <el-form-item class="form-sbt">
          <el-button size="medium" type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCaptchaApi, loginApi } from '@/api/manager-api';
export default {
  name: 'Login',
  data() {
    return {
      // 验证码标签
      captcha: '',
      loginForm: {
        username: '',
        password: '',
        captcha: ''
      },
      rules: {
        username: {
          required: true,
          message: '请输入登录账号',
          trigger: ['blur', 'change']
        },
        password: {
          required: true,
          message: '请输入登录密码',
          trigger: ['blur', 'change']
        },
        code: {
          required: true,
          message: '请输入验证码',
          trigger: ['blur', 'change']
        }
      }
    };
  },
  created() {
    this.getCaptcha();
  },
  methods: {
    // 获取验证码
    async getCaptcha() {
      try {
        let {
          code,
          data: { img }
        } = await getCaptchaApi();
        if (code !== 200) return;
        this.captcha = img;
      } catch (err) {
        console.warn(err);
      }
    },
    // 验证表单
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return false;

        try {
          const { code, data } = await loginApi(this.loginForm);
          if (code !== 200) return;

          this.$store.commit('user/SET_TOKEN', data);

          // 获取角色并存储角色信息
          await this.$store.dispatch('user/getUserInfo');
          await this.$store.dispatch('permission/generateRoutes');

          this.$router.push({ path: '/dashboard' });
        } catch (err) {
          console.warn(err);
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
a {
  color: #999;
  text-decoration: none;
  transition: all 0.3s linear;
}
.login {
  width: 360px;
  margin: 10% auto 0;

  .titles {
    text-align: center;
    line-height: 1.2;
  }
  .title-main {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-size: 25px;
    img {
      width: 48px;
    }

    sup {
      font-size: 15px;
    }
  }
  .title-sub {
    margin-top: 10px;
    font-size: 14px;
    color: #999;
    letter-spacing: 5px;
  }

  .el-form-item {
    margin: 20px 0;
  }

  .form-code {
    .el-input {
      width: 235px !important;
      margin-right: 5px;
    }
    .codeimg {
      display: inline-block;
      height: 36px;
      vertical-align: top;
      cursor: pointer;
    }
  }

  .form-sbt .el-button--primary {
    width: 100%;
  }
}
</style>
